package com.bum.game.web.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.bum.game.bo.Game;
import com.google.appengine.api.datastore.Entity;

public class GameManageController extends SimpleFormController {

	public GameManageController() {
		setCommandClass(GameCommand.class);
		setValidator(new GameValidator());
	}
	
	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = super.handleRequestInternal(request, response);

		String code = StringUtils.trimToNull(request.getParameter("c"));
		if (code != null) {
			Entity game = Game.get(code);
			if (game != null) {
				mv.addObject("game", game.getProperties());
			}
		}
		
		return mv;
	}

	@Override
	protected ModelAndView onSubmit(Object command) throws Exception {
		ModelAndView mv = super.onSubmit(command);
		
		GameCommand cmd = (GameCommand) command;
		Game.save(cmd.getCode(), cmd.getTitle(), cmd.getSize(), cmd.getJar(), cmd.getJad(), cmd.getImg(), cmd.getIntro(), cmd.getContent());
		
		return mv;
	}
	
	class GameValidator implements Validator {
		@SuppressWarnings("rawtypes")
		public boolean supports(Class clazz) {
			return clazz.equals(GameCommand.class);
		}

		public void validate(Object obj, Errors errors) {
			if (errors.hasErrors()) {
				return;
			}
			
			GameCommand cmd = (GameCommand) obj;
			
			if (StringUtils.isBlank(cmd.getCode())) {
				errors.reject("game.code.empty", "Phải nhập code");
			}
		}
	}
	
	public static class GameCommand {
		private String code;
		private String title;
		private int size;
		private String jar;
		private String jad;
		private String img;
		private String intro;
		private String content;

		public String getTitle() {
			return title;
		}

		public void setTitle(String title) {
			this.title = title;
		}

		public String getCode() {
			return code;
		}

		public void setCode(String code) {
			this.code = code;
		}

		public int getSize() {
			return size;
		}

		public void setSize(int size) {
			this.size = size;
		}

		public String getJar() {
			return jar;
		}

		public void setJar(String jar) {
			this.jar = jar;
		}

		public String getJad() {
			return jad;
		}

		public void setJad(String jad) {
			this.jad = jad;
		}

		public String getImg() {
			return img;
		}

		public void setImg(String img) {
			this.img = img;
		}

		public String getIntro() {
			return intro;
		}

		public void setIntro(String intro) {
			this.intro = intro;
		}

		public String getContent() {
			return content;
		}

		public void setContent(String content) {
			this.content = content;
		}
	}
	
}
